local jw_tongmeng = fk.CreateSkill {
    name = "jw_tongmeng",
    anim_type = "offensive",
}

Fk:loadTranslationTable {
    ["jw_tongmeng"] = "统盟",
    [":jw_tongmeng"] = "每当其他角色死亡时，你可以将势力改为场上现存的势力，然后令与你相同势力的其他角色选择一项：<br>" ..
        "1.交给你两张手牌；<br>2.失去一点体力；<br>3.弃置所有装备牌。",

    ["tm_givecard"] = "交出去两张手牌",
    ["tm_losehp"] = "失去一点体力",
    ["tm_discards"] = "弃置所有装备牌",

    ["#jw_tongmeng-give"] = "统盟：请选择交给 %src 的两张牌",


    ["$jw_tongmeng1"] = "董贼祸国，忠良之裔，当手举义旗！",
    ["$jw_tongmeng2"] = "袁氏公侯之家，匡汉大业义不容辞！",
}


jw_tongmeng:addEffect(fk.Death, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(jw_tongmeng.name)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local kingdom = {}
        for _, id in ipairs(room.alive_players) do
            if not table.contains(kingdom, id.kingdom) then
                table.insertIfNeed(kingdom, id.kingdom)
            end
        end
        local choice = room:askToChoice(player, {
            skill_name = jw_tongmeng.name,
            choices = kingdom,
            prompt = "统盟：请选择要变更的势力" 
        })
        if choice == nil then
            choice = table.random(kingdom)
        end
        room:changeKingdom(player, choice, true)
        for _, p in ipairs(room:getOtherPlayers(player)) do
            if p.kingdom == player.kingdom then
                local all_chs = { "tm_givecard", "tm_losehp", "tm_discards" }
                local chs = { "tm_losehp" }
                if #p:getCardIds("h") > 1 then
                    table.insertIfNeed(chs, "tm_givecard")
                end
                if #p:getCardIds("e") > 0 then
                    table.insertIfNeed(chs, "tm_discards")
                end
                local chc = room:askToChoice(p, {
                    skill_name = jw_tongmeng.name,
                    choices = chs,
                    all_choices = all_chs,
                })
                if chc == nil or chc == "tm_losehp" then
                    room:loseHp(p, 1, jw_tongmeng.name)
                end
                if chc == "tm_givecard" then
                    local cards = room:askToCards(p, {
                        min_num = 2,
                        max_num = 2,
                        include_equip = false,
                        skill_name = jw_tongmeng.name,
                        prompt = "#jw_tongmeng-give:" .. player.id,
                        cancelable = false,
                    })
                    if #cards == 2 then
                        room:moveCardTo(cards, Player.Hand, player, fk.ReasonGive, jw_tongmeng.name, nil, false, p)
                    else
                        room:loseHp(p, 1, jw_tongmeng.name)
                    end
                end
                if chc == "tm_discards" then
                    p:throwAllCards("e")
                end
            end
        end
    end,
})

return jw_tongmeng
